package com.indusfo.spc.service.impl;

import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.ProcessOfflineMapper;
import com.indusfo.spc.pojo.ProcessOffline;
import com.indusfo.spc.service.ProcessOfflineService;
import com.indusfo.spc.vo.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ProjectName: jpa05
 * @Package: com.indusfo.spc.service.impl
 * @ClassName: ProcessOfflineServiceImpl
 * @Author: 熊冰
 * @Description: 新流程维修
 * @Date: 2019/10/16 13:18
 * @Version: 1.0
 */

@Service
public class ProcessOfflineServiceImpl implements ProcessOfflineService {
	private static final Logger logger = LoggerFactory.getLogger(ProcessOfflineServiceImpl.class);
	@Resource
	private ProcessOfflineMapper processOfflineMapper;

	/**
	 * 分页查询 pagesize pageindex
	 * 模糊查询 dim
	 * ID查询  Pid
	 * 返回总数 count
	 * @param processOffline
	 * @return
	 */
	@Override
	public JSONObject queryProcessOffline(ProcessOffline processOffline) {
		try {
			//获取页面的数据个数
			Integer pagesize = processOffline.getPagesize();
			//获取查询的页数
			Integer pageindex = processOffline.getPageindex();

			if(pagesize!=null && pageindex !=null){
				processOffline.setIncept(pagesize*(pageindex-1));
			}
			List<ProcessOffline> listProcessBom=processOfflineMapper.queryProcessOffline(processOffline);
			if(listProcessBom.isEmpty()){
				return  JSONObject.oK("没有查询到相关消息",listProcessBom,0);
			}
			//如果页数和页面数量都不为空的情况下进行计数
			Integer count= processOfflineMapper.countProcessOffline(processOffline);
			return JSONObject.oK("查询成功",listProcessBom,count);
		} catch (GlobalException e) {
			// 捕获异常,打印并返回数
			logger.error(e.getMessage(), e);
			return JSONObject.build(500, e.getMessage());

		}
	}

	/**
	 * 非空判断 长度验证   checkParam
	 *      查重判断 checkParamsNotRepeat
	 * 默认值赋值 setDataState(1)
	 * 返回总数 count
	 * @param processOffline
	 * @return
	 */
	@Override
	public JSONObject insertProcessOffline(ProcessOffline processOffline) {
		/**
		 * 这里还要判断哪些不为空
		 */
		try{
			if(processOffline.getlProOffline()!=null){
				throw new ModifyFailedException("新增时ID不能存在");
			}
			if(processOffline.getDataState()==null){
				processOffline.setDataState(1);
			}
			checkParam(processOffline);
			processOffline.setDataState(1);
			Integer row=processOfflineMapper.insertProcessOffline(processOffline);
			if(row==0){
				throw new ModifyFailedException("新增不良现象失败");
			}
			return JSONObject.oK("新增成功");
		}catch(GlobalException e) {//捕获自定义异常
			logger.error(e.getMessage());
			return JSONObject.build(500, e.getMessage());
		}
	}

	@Override
	public JSONObject deleteProcessOffline(List<Integer> processOfflineIds, Integer IDataState) {
		try{
			//判断传入的参数是否为null
			if(IDataState == null) {
				//为null抛出异常
				throw new ParamsErrorException("数据状态不能为空");
			}else if(IDataState != 1 && IDataState != 2 && IDataState != 3) {
				throw new ParamsErrorException("数据状态错误");
			}
			if(processOfflineIds == null) {
				throw new ParamsErrorException("请选择设备资料");
			}
			//调用删除的接口
			int row = processOfflineMapper.deleteProcessOffline(processOfflineIds,IDataState);
			//声明msg字符串，用来存抛出的异常
			String msg = "";
			if (row == 0) {
				// 判断传入的数据状态参数lDataState,返回相应信息
				switch (IDataState) {
					case 1:
						throw new ModifyFailedException("启用失败");
					case 2:
						throw new ModifyFailedException("删除失败");
					case 3:
						throw new ModifyFailedException("停用失败");
					default:
						break;
				}
			} else {
				switch (IDataState) {
					case 1:
						msg = "启用成功";break;
					case 2:
						msg = "删除成功";break;
					case 3:
						msg = "停用成功";break;
					default:
						break;
				}
			}
			return JSONObject.oK(msg);
		}catch(GlobalException e) {
			logger.error(e.getMessage());
			return JSONObject.build(500, e.getMessage());
		}
	}

	@Override
	public JSONObject updateProcessOffline(ProcessOffline processOffline) {
		try {
			//判断传入的参数是否为null
			if(processOffline.getOfflineId()==null) {
				//为null则抛出异常
				throw new ParamsErrorException("修改维修下线id不能为空");
			}
			if(processOffline.getDataState()==null){
				processOffline.setDataState(1);
			}
			checkParam(processOffline);

			//调用修改的接口
			int row = processOfflineMapper.updateProcessOffline(processOffline);
			if(row == 0) {
				throw new ModifyFailedException("数据修改失败");
			}
			return JSONObject.oK("数据修改成功");
		}catch (GlobalException e) {//捕获自定义异常
			logger.error(e.getMessage());
			return JSONObject.build(500, e.getMessage());
		}
	}

	@Override
	public JSONObject batchInsertProcessOffline(List<Integer> offlineIds, Integer lProVer) {
		try{



			if(lProVer==null){
				throw new ModifyFailedException("版本编号不存在");
			}
			if(offlineIds==null){
				throw new ModifyFailedException("维修流程编号不存在");
			}
			Integer row=0;
			for(int i=0;i<offlineIds.size();i++){
				ProcessOffline processOffline=new ProcessOffline();
				processOffline.setOfflineId(offlineIds.get(i));
				processOffline.setlProVer(lProVer);
				processOffline.setDataState(1);
				Integer Counts = processOfflineMapper.checkProcessOffline(processOffline);
				if (Counts !=0) {
					throw new ParamsErrorException("该数据已存在");
				}else{
					row=row+processOfflineMapper.insertProcessOffline(processOffline);
				}
			}
			if(row==0){
				throw new ModifyFailedException("新增失败");
			}
			return JSONObject.oK("新增成功",row);
		}catch(GlobalException e) {//捕获自定义异常
			logger.error(e.getMessage());
			return JSONObject.build(500, e.getMessage());
		}
	}



	public void checkParam(ProcessOffline processOffline){
//        if(processOffline.getModerno()==null){
//            throw new ParamsErrorException("任务单号不能为空");
//        }
//        if(processOffline.getNmorderno()==null){
//            throw new ParamsErrorException("新任务单号不能为空");
//        }
//
//        if(StringUtils.isEmpty(processOffline.getMatId())){
//            throw new ParamsErrorException("产品编码不能为空");
//        }
//        if(StringUtils.isEmpty(processOffline.getNmatId())){
//            throw new ParamsErrorException("新产品编码不能为空");
//        }
//		if(processOffline.getlProFlow()==null){
//			throw new ParamsErrorException("工艺流程ID不能为空");
//		}
		if(processOffline.getOfflineId()==null){
			throw new ParamsErrorException("维修下线ID不能为空");
		}
		if(processOffline.getlProVer()==null){
			throw new ParamsErrorException("版本ID不能为空");
		}
		// 名称不能重复
		Integer Counts = processOfflineMapper.checkProcessOffline(processOffline);
		if (Counts !=0) {
			throw new ParamsErrorException("该维修下线已存在");
		}
	}
}
